$(function () {
    $(".loadingMarsk").css({
        "width":window.screen.availWidth,
        "height":window.screen.availHeight,
    });
    new FastClick(document.body);
    //获取token
    var token = localStorage.token;
    console.log(token);
    if (!token) {
        location.href = "../loginAndReg/login.html";
        return false;
    };
    //加载收藏列表
    function loadCollectionList() {
        $.ajax({
            type: "get",
            url: domain.test + "/getcollection",
            // url: domain.online + "/love/list",
            beforeSend: function (xhr) {
                xhr.setRequestHeader("token", token)
            },
            success: function (response) {
                if (response.data.rtnCode === 200) {
                    // if(response.rtnCode===200) {
                    var collectionList = response.data.data;
                    // var collectionList = response.data;
                    $(".productList").html(template("productList", { collectionList: collectionList }));
                    pull.pullDownSuccess(); //刷新成功
                    $(".loadingMarsk").css("display","none");
                }
                if (response.data.rtnCode === 401) {
                    // if(response.rtnCode===401) {
                    var outTimeCode = response.data.rtnCode;
                    // var outTimeCode = response.rtnCode;
                    location.href = "../../index.html?outTimeCode=" + outTimeCode;
                }
            }
        });
    }
    loadCollectionList();
    var pull = new Pull($(".productList"), {
        onPullDown: function () {
            loadCollectionList();
        }
    });
    //删除收藏商品
    $(".productList").on("click", ".delIcon", function (e) {
        e.stopPropagation();
        var parentDom = $(this).parent().parent();
        var pId = parseInt(parentDom.attr("data-loveId"));
        parentDom.find(".delBtn").toggle();
        parentDom.find(".delBtn").on("click", function (e) {
            e.stopPropagation();
            $.ajax({
                type: "delete",
                url: domain.test + "/delcollection?id=" + pId,
                // url: domain.inline + "/love/del?id=" + pId,
                beforeSend: function (xhr) {
                    $(".loadingMarsk").css("background-color","rgba(0,0,0,.3)");
                    $(".loadingMarsk").css("display","block");
                    xhr.setRequestHeader("token", token);
                },
                success: function (response) {
                    console.log(response);
                    if (response.data.rtnCode === 200) {
                    // if(response.rtnCode===200) {
                        $(".loadingMarsk").css("display","block");                    
                        location.reload();
                    }
                    if (response.data.rtnCode === 401 || response.data.rtnCode === 400) {
                    // if (response.rtnCode === 401 || response.rtnCode === 400) {
                        var outTimeCode = response.data.rtnCode;
                        // var outTimeCode = response.rtnCode;
                        location.href = "../../index.html?outTimeCode=" + outTimeCode;
                    }
                }
            });
        });
    });
    //详情跳转
    $(".productList").on("click", ".productItem", function () {
        var id = $(this).attr("data-id");
        location.href = "../classify/product.html?id=" + id;
    });
});